package com.flyou.ccnucenter.ui.activity.henuBeside.anonymousLove;

import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.adapter.LoveHeadViewAdapter;
import com.flyou.ccnucenter.domain.AnonymousLove;
import com.flyou.ccnucenter.domain.MyUser;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.utils.BmobErrorCodeUtils;
import com.flyou.ccnucenter.widget.ClearEditText;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

public class AddLoveActivity extends BaseActivity1 implements View.OnClickListener, TextWatcher {
    private int selectionStart;
    private int selectionEnd;
    private int totalNum = 512;
    private CharSequence tempCount;
    private RelativeLayout mTitlebarRlBack;
    private ClearEditText et_loveName;

    private TextView mTitlebarTvTitle;
    private TextView titleBarRight;
    private ImageView headView;
    private EditText content;
    private TextView textNum;
    private List<Integer> headViewImageId;
    private  int currentHeadImageId=0;


    @Override
    protected void initView() {
        setContentView(R.layout.activity_add_love);

        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);

        titleBarRight = (TextView) findViewById(R.id.titlebar_tv_text_right);
        headView = (ImageView) findViewById(R.id.iv_love_headView);
        content = (EditText) findViewById(R.id.tv_love_content);
        textNum = (TextView) findViewById(R.id.tv_love_nubmer);
        et_loveName= (ClearEditText) findViewById(R.id.et_loveName);


    }

    @Override
    protected void initDate() {

        mTitlebarTvTitle.setText("大声喊出你的爱");
        titleBarRight.setText("发布");
        titleBarRight.setVisibility(View.VISIBLE);
        headViewImageId=new ArrayList<>();
        headViewImageId.add(R.drawable.man1);
        headViewImageId.add(R.drawable.man2);
        headViewImageId.add(R.drawable.man3);
        headViewImageId.add(R.drawable.man4);
        headViewImageId.add(R.drawable.woman1);
        headViewImageId.add(R.drawable.woman2);
        headViewImageId.add(R.drawable.woman3);
        headViewImageId.add(R.drawable.woman4);
    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
        titleBarRight.setOnClickListener(this);
        content.addTextChangedListener(this);
        headView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_rl_back:
                setResult(RESULT_CANCELED);
                finish();
                break;
            case R.id.titlebar_tv_text_right:
                if (TextUtils.isEmpty(et_loveName.getText().toString().trim())) {
                    Toast.makeText(this, "表白对象不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(content.getText().toString().trim())) {
                    Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                addLove();
                break;
            case R.id.iv_love_headView:

                AlertDialog.Builder dialog = new AlertDialog.Builder(AddLoveActivity.this);
                View view = View.inflate(AddLoveActivity.this, R.layout.view_add_love_headview_dialog, null);
                dialog.setView(view);
                final AlertDialog show = dialog.show();
                GridView headview = (GridView) view.findViewById(R.id.grid_headview);
                LoveHeadViewAdapter headViewAdapter =new LoveHeadViewAdapter(AddLoveActivity.this,headViewImageId);
                headview.setAdapter(headViewAdapter);
                headview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        show.dismiss();
                        headView.setImageResource(headViewImageId.get(position));
                        currentHeadImageId=position;
                    }
                });

                break;
        }
    }


    /*
     * 获取 笔记数据
     */
    private void addLove() {
        MyUser currentUser = BmobUser.getCurrentUser(AddLoveActivity.this, MyUser.class);

        AnonymousLove love=new AnonymousLove(currentUser,content.getText().toString().trim(),1,et_loveName.getText().toString().trim(),currentHeadImageId);
        love.save(AddLoveActivity.this, new SaveListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(AddLoveActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                finish();

            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(AddLoveActivity.this, BmobErrorCodeUtils.getMessage(i, s), Toast.LENGTH_SHORT).show();
            }
        });

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {


    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        tempCount = s;
    }

    @Override
    public void afterTextChanged(Editable s) {
        int number = totalNum - s.length();
        textNum.setText(totalNum - number + "/" + totalNum);
        selectionStart = content.getSelectionStart();
        selectionEnd = content.getSelectionEnd();
        if (tempCount.length() > totalNum) {
            s.delete(selectionStart - 1, selectionEnd);
            int tempSelection = selectionEnd;
            content.setText(s);
            content.setSelection(tempSelection);//设置光标在最后
        }
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        super.onBackPressed();
    }
}
